﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Sol_File.Controllers
{
    public class DownloadController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet("ExportFile")]
        // public async Task<IActionResult> ExportFile()
        public IActionResult ExportFile()
        {
            string path = @"F:\DownLoad\新建文件夹\新建文本文档.txt";
            string fileName = "新建文本文档.txt";
            Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

            //获取文件扩展名 
            string fileExt = Path.GetExtension(path);
            //获取文件的ContentType       
            var contentType = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[fileExt];  //.net core 版本 
            //var contentType2 = System.Web.MimeMapping.GetMimeMapping(fileExt); //.net framework 版本
            //var contentType3 = MimeTypes.MimeTypeMap.GetMimeType(fileExt);  //需Neget MediaTypeMap 或者  MediaTypeMap.Core 程序包

            return File(fs, contentType, fileName);
        }

    }
}